﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;

public partial class Control_Clients_ClientList : System.Web.UI.UserControl
{
    private int acc_id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        //acc_id = 1;

        if (!IsPostBack)
        {
            if (Session["ACC_ID"] != null)
            {
                loadGridView();
            }
            else
            {
                Response.Redirect("AdminLogin.aspx");
            }

        }
    }

    private void loadGridView()
    {
        CLIENTSDataAPI cln = new CLIENTSDataAPI();
        acc_id = int.Parse(Session["ACC_ID"].ToString());
        DataTable dt = new DataTable();
        dt = cln.getByACC_ID(acc_id);
        if (dt != null)
        {
            if (dt.Rows.Count <= 10)
            {
                int currentRows = dt.Rows.Count;
                int blankRows = 11 - currentRows;
                for (int i = 0; i <= blankRows; i++)
                {

                    dt.Rows.Add(0, 0, "", "", "", "");

                }
            }
            grvShow.DataSource = dt;
            grvShow.DataBind();
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "There is no database";
        }
                
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        string searchPhrase = tbxSearch.Text;
        if (searchPhrase != "")
        {
            loadSearchResult(searchPhrase.Trim());
        }
        else
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please enter search phrase!');", true);

            CLIENTSDataAPI cln = new CLIENTSDataAPI();
            acc_id = int.Parse(Session["ACC_ID"].ToString());
            DataTable dt = new DataTable();

            dt = cln.getByACC_ID(acc_id);
            grvShow.DataSource = dt;
            grvShow.DataBind();
            
        }
    }

    private void loadSearchResult(string searchPhrase)
    {
        try
        {            
            CLIENTSDataAPI cln = new CLIENTSDataAPI();
            acc_id = int.Parse(Session["ACC_ID"].ToString());
            DataTable dt = new DataTable();

            dt = cln.getSearchResult(acc_id, searchPhrase.ToUpper());

            if (dt.Rows.Count >= 1)
            {
                grvShow.DataSource = dt;
                grvShow.DataBind();
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = tbxSearch.Text + " Not found";
            }
            
        }
        catch (Exception ex)
        {
            ex.ToString();            
        }
    }
    
}
